<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');


class Mstatus extends Admin_Controller
{
    
    function __construct()
    {
        parent::__construct();
        $this->admin_navigation->parent_active('categories');
        $this->load->model('categories/Mstatus_model');
    }
    
    function index()
    {
        $columns = array(
            array(
                'name' => 'ID',
                'type' => 'id',
                'filter' => 'id',
                'index' => 'id',
                'sort_column' => true
            ),
            array(
                'name' => 'Name',
                'type' => 'text',
                'filter' => 'name',
                'index' => 'name',
                'sort_column' => true
            ),
            array(
                'name' => 'Trạng thái',
                'type' => 'publish',
                'filter' => 'status',
                'index' => 'status',
                'options' => array(
                    '0' => 'Tắt',
                    '1' => 'Bật'
                )
            )
        );
        $this->grid->columns($columns);
        $this->grid->datasource('categories/Mstatus_model', 'get_objects');
        
        $this->grid->set_title('status Manager');
        $this->grid->add_button('Thêm mới', admin_url('*/mstatus/add'), 'add');
        $this->grid->set_row_action('edit');
        
        $this->grid->total_rows($this->Mstatus_model->get_objects_count());
        $this->grid->add_mass_action('delete', array(
            'label' => 'Delete',
            'url' => admin_url('*/mstatus/delete'),
            'confirm' => 'Are you sure?'
        ));
        $this->grid->add_mass_action('public', array(
            'label' => 'Public',
            'url' => admin_url('*/mstatus/status_public')
        ));
        $this->grid->add_mass_action('unpublic', array(
            'label' => 'Unpublic',
            'url' => admin_url('*/mstatus/status_unpublic')
        ));
        
        if ($this->input->is_ajax()) {
            $this->output->set_output($this->grid->display());
        } else {
            $this->load->view('admin/grid');
        }
    }
    
    function edit()
    {
        $id = $this->input->get_param('id', false);
        if ($id) {
            $data = $this->Mstatus_model->get_object_by_id($id);
            $this->form->set_values($data);
        }
        $this->form->set_form_attributes(array(
            'id' => 'form',
            'action' => admin_url('*/mstatus/edit_post', array(
                'id' => $id
            ))
        ));
        
        $this->form->add_filedset('ganeral', array(
            'legend' => 'Ganeral'
        ));
        
        $this->form->add_filed('name', 'text', array(
            'label' => $this->__('Name'),
            'name' => 'name',
            'required' => true
        ));
        
        $this->form->add_filed('alias', 'alias', array(
            'label' => $this->__('Alias'),
            'name' => 'alias'
        ));
        
        $this->form->add_filed('status', 'select', array(
            'label' => $this->__('Status'),
            'name' => 'status',
            'values' => array(
                'Tắt',
                'Bật'
            )
        ));
        
        $this->load->view('admin/form', array(
            'form' => $this->form->to_html()
        ));
    }
    
    function add()
    {
        $this->_forward('edit');
    }
    
    function delete()
    {
        $cids = explode(',', $this->input->get_param('id', false));
        foreach ($cids as $cid) {
            $this->Mstatus_model->delete($cid);
        }
        $this->_redirect(admin_url('*/mstatus/index'));
    }
    
    function status_public()
    {
        $cids = explode(',', $this->input->get_param('id', false));
        foreach ($cids as $cid) {
            $this->Mstatus_model->change_status($cid, 1);
        }
        $this->_redirect(admin_url('*/mstatus/index'));
    }
    
    function status_unpublic()
    {
        $cids = explode(',', $this->input->get_param('id', false));
        foreach ($cids as $cid) {
            $this->Mstatus_model->change_status($cid, 0);
        }
        $this->_redirect(admin_url('*/mstatus/index'));
    }
    
    function edit_post()
    {
        $id = $this->input->get_param('id', false);
        
        if (($data = $this->input->post())) {
            if ($id) {
                $this->Mstatus_model->update($data, $id);
            } else {
                $this->Mstatus_model->insert($data);
            }
        }
        $this->_redirect(admin_url('*/mstatus/index'));
    }
}